const app = getApp();
const api = require('../../utils/api');

Page({
    data: {
        shopId: 0,
        transactionList: [], //订单列表
        pageNum: 1,
        pageSize: 15,
        total: 0,
        noContent: false, //无数据
        isFinished: false, //加载结束
    },
    onLoad(options) {
        this.setData({
            shopId: options.shopId
        });

        this.getData();
    },
    onShow() {

    },

    getData() { //获取列表数据
        let params = {
            pageNum: this.data.pageNum,
            pageSize: this.data.pageSize
        }
        api.post('/user/account/order/page', params)
            .then(res => {
                let list = res.content;
                list.forEach(item => {
                    //日期格式化
                    item.createTime = app.filterDate(item.createTime);
                    //金额格式化
                    item.amountChange = item.amountChange.toFixed(2);
                })
                let newList = [...this.data.transactionList, ...list];

                this.setData({
                    transactionList: newList,
                    total: res.total
                });

                //是否无内容标识
                if(this.data.transactionList.length == 0) {
                    this.setData({
                        noContent: true
                    })
                } else {
                    this.setData({
                        noContent: false
                    });
                }
                //是否加载完毕标识
                if(this.data.transactionList.length >= this.data.total) {
                    this.setData({
                        isFinished: true
                    })
                } else {
                    this.setData({
                        isFinished: false
                    })
                }
                wx.stopPullDownRefresh();
            })
    },
    onPullDownRefresh() {

    },
    onReachBottom() {

    },

    onPullDownRefresh() {
        //下拉刷新
        this.setData({
            transactionList: [],
            pageNum: 1,
            total: 0,
            isFinished: false,
        });
        this.getData();
    },
    onReachBottom() {
        //加载更多
        if(!this.data.isFinished) {
            this.setData({
                pageNum: this.data.pageNum+1,
            });
            
            setTimeout(() => {
                this.getData();
            }, 800);
        }
    },
})